package leetcode.l0615;

/**
 * @author Retain
 * @date 2021/6/15 18:05
 */
public class Easy_山脉数组的峰顶索引_852 {

    public static void main(String[] args) {
        peakIndexInMountainArray(new int[]{3,4,5,1});
    }
    /**
     * 使用二分法，判断中间数左右两边的值是否存在顺序
     * @param arr
     * @return
     */
    public static int peakIndexInMountainArray(int[] arr) {
        int left = 0;
        int right = arr.length - 1;
        while (left < right) {
            int mid = left + ((right - left) >> 1);
            if (arr[mid] > arr[mid+1]) {
                // mid 处于顶峰
                if (arr[mid] > arr[mid - 1]) {
                    return mid;
                }else {
                    // mid处于反向递增序列中
                    right = mid;
                }
            }else {
                // 右边比mid大，不可能为顶峰
                left = mid + 1;
            }
        }
        return left;
    }
}
